package wang.hex;


import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Author fox
 * @Class AbstractTracerMethodVisitor
 * @Description
 * @Date 2020/12/24 12:38
 */
public abstract class LoggerTracerMethodVisitor implements TracerMethodVisitor {
    private final static String format = "%s %s - %s [%s] [%s] [%s ms] \n";
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

    private String clazz;
    private Object realObject;
    private Method method;
    private Object params;
    private Exception e;
    private Object ret;
    private long start;

    static {
        TimeZone timeZone = TimeZone.getTimeZone("GMT+8");

        TimeZone.setDefault(timeZone);
    }

    public boolean accept(String clazz, String method) {
        this.clazz = clazz;
        return true;
    }


    public abstract void logger(String line);


    public void before(Object realObject, Method method, Object[] params) {
        this.realObject = realObject;
        this.params = params;
        this.start = System.currentTimeMillis();
        this.method = method;

    }

    public void end(Object ret) {
        this.ret = ret;
        logger(SimpleLogger.formatLogger(format, sdf.format(new Date()), realObject.getClass().getSimpleName(), method.getName(), params, ret, "" + (System.currentTimeMillis() - start)));
    }

    public void exception(Exception e) {
        logger(SimpleLogger.formatLogger("[%s]", e));

        this.e = e;
    }




}
